package com.dinzeer.sjapadder.se;

import com.dinzeer.legendreliclib.lib.util.slashblade.SlashBladeUtil;
import com.dinzeer.sjapadder.register.SjaSpecialEffectRegsitry;
import mods.flammpfeil.slashblade.event.SlashBladeEvent;
import mods.flammpfeil.slashblade.item.ItemSlashBlade;
import mods.flammpfeil.slashblade.registry.specialeffects.SpecialEffect;
import net.minecraft.util.Mth;
import net.minecraft.world.damagesource.DamageSource;
import net.minecraft.world.damagesource.DamageTypes;
import net.minecraft.world.effect.MobEffectInstance;
import net.minecraft.world.effect.MobEffects;
import net.minecraft.world.entity.Entity;
import net.minecraft.world.entity.LivingEntity;
import net.minecraft.world.entity.player.Player;
import net.minecraftforge.event.entity.living.LivingHurtEvent;
import net.minecraftforge.eventbus.api.EventPriority;
import net.minecraftforge.eventbus.api.SubscribeEvent;
import net.minecraftforge.fml.common.Mod;

@Mod.EventBusSubscriber
public class LifeForHealth extends SpecialEffect {
    // 曲线参数配置
    private static final float MIN_DAMAGE = 0.1f;      // 开始生效的最小伤害
    private static final float MAX_DAMAGE = 30.0f;     // 达到最大减伤时的伤害阈值
    private static final float MIN_REDUCTION = 0.10f;  // 最小减伤比例 (10%)
    private static final float MAX_REDUCTION = 0.85f;  // 最大减伤比例 (75%)
    private static final float CURVE_FACTOR = 2.0f;    // 曲线陡峭因子

    public LifeForHealth() {
        super(30);
    }

    @SubscribeEvent(priority = EventPriority.LOWEST)
    public static void onUpdateEvent(LivingHurtEvent event) {
        LivingEntity entity = event.getEntity();

        // 检查主手是否为拔刀剑并拥有特殊效果
        if (!(entity.getMainHandItem().getItem() instanceof ItemSlashBlade) ||
                !SlashBladeUtil.hasSpecialEffect(entity.getMainHandItem(), SjaSpecialEffectRegsitry.LifeForHealth)) {
            return;
        }

        // 排除某些伤害类型（如虚空伤害）
        DamageSource source = event.getSource();
        if (source.is(DamageTypes.IN_WALL) || source.is(DamageTypes.FELL_OUT_OF_WORLD)) {
            return;
        }

        float originalDamage = event.getAmount();
        float reducedDamage = calculateReducedDamage(originalDamage);

        // 应用减伤
        event.setAmount(reducedDamage);

        // 添加视觉反馈
        if (entity instanceof Player) {
            Player player = (Player) entity;
            player.hurtMarked = true; // 显示受伤动画
            player.addEffect(new MobEffectInstance(
                    MobEffects.GLOWING, 20, 0, false, false
            ));
        }
    }

    /**
     * 基于曲线算法的动态减伤计算
     *
     * @param damage 原始伤害值
     * @return 减伤后的实际伤害值
     */
    private static float calculateReducedDamage(float damage) {
        if (damage <= MIN_DAMAGE) {
            return damage; // 低于阈值不减免
        }
        System.out.println("莉芙：原伤害"+damage);
        // 计算伤害在曲线上的位置 (0.0 - 1.0)
        float normalizedDamage = Mth.clamp(
                (damage - MIN_DAMAGE) / (MAX_DAMAGE - MIN_DAMAGE),
                0.0f, 1.0f
        );

        // 使用S形曲线计算减伤比例 (sigmoid变体)
        float reductionFactor = MIN_REDUCTION +
                (MAX_REDUCTION - MIN_REDUCTION) *
                        (1.0f - (float)Math.pow(1.0f - normalizedDamage, CURVE_FACTOR));

        // 确保减伤比例在合理范围内
        float actualReduction = Mth.clamp(reductionFactor, MIN_REDUCTION, MAX_REDUCTION);
        System.out.println("莉芙：现伤害"+damage * (1.0f - actualReduction));
        // 计算最终伤害
        return damage * (1.0f - actualReduction);
    }


}